package com.xxgc.demo.utils;

import com.iflytek.cloud.speech.*;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;

/**
 * @Author:Sjy
 * @Date :2023/9/26 - 09 - 26 - 14:46
 * 语音合成工具类
 */
@Slf4j
public class SpeechSynthesisUtil {


    public static void main(String[] args) {
        SpeechSynthesisUtil synthesisUtil = new SpeechSynthesisUtil();
        synthesisUtil.speak("吴彦祖你该起床啦，今天彭于晏邀请你和他一起去上课");
    }

    // 语音合成对象
    private SpeechSynthesizer mTts;

    /**
     * 无参构造函数做初始化配置
     */
    public SpeechSynthesisUtil() {
        // 替换成您申请的APPID
        SpeechUtility.createUtility( SpeechConstant.APPID +"=374641a2");
        // 初始化合成对象
        mTts = SpeechSynthesizer.createSynthesizer();
        if(mTts != null){
            //音色
            mTts.setParameter(SpeechConstant.VOICE_NAME,"xiaoyan");
            //语速
            mTts.setParameter(SpeechConstant.SPEED,"50");
            //音量
            mTts.setParameter(SpeechConstant.VOLUME,"100");

        }else{
            log.info("引擎初始化失败，请检查jar包和dll文件");
        }

    }

    /**
     * 传文字进来系统会自动说话
     * @param message
     */
    public void speak(String message){
        //使用合成监听器
        mTts.startSpeaking(message, mSynListener);
    }

    /**
     * 合成监听器
     */
    private SynthesizerListener mSynListener = new SynthesizerListener() {

        @Override
        public void onSpeakBegin() {}
        @Override
        public void onBufferProgress(int progress, int beginPos, int endPos,
                                     String info) {
            log.info("--onBufferProgress--progress:" + progress
                    + ",beginPos:" + beginPos + ",endPos:" + endPos);
        }
        @Override
        public void onSpeakPaused() {}
        @Override
        public void onSpeakResumed() {}
        @Override
        public void onSpeakProgress(int progress, int beginPos, int endPos) {}
        @Override
        public void onCompleted(SpeechError error) {}

        @Override
        public void onEvent(int eventType, int arg1, int arg2, int arg3, Object obj1, Object obj2) {
            if( SpeechEvent.EVENT_TTS_BUFFER == eventType ){
                log.info( "onEvent: type="+eventType
                        +", arg1="+arg1
                        +", arg2="+arg2
                        +", arg3="+arg3
                        +", obj2="+(String)obj2 );
                ArrayList<?> bufs = null;
                if( obj1 instanceof ArrayList<?> ){
                    bufs = (ArrayList<?>) obj1;
                }else{
                    log.info( "onEvent error obj1 is not ArrayList !" );
                }//end of if-else instance of ArrayList

                if( null != bufs ){
                    for( final Object obj : bufs ){
                        if( obj instanceof byte[] ){
                            final byte[] buf = (byte[]) obj;
                            log.info( "onEvent buf length: "+buf.length );
                        }else{
                            log.info( "onEvent error element is not byte[] !" );
                        }
                    }//end of for
                }//end of if bufs not null
            }//end of if tts buffer event
            //以下代码用于调试，如果出现问题可以将sid提供给讯飞开发者，用于问题定位排查
			/*else if(SpeechEvent.EVENT_SESSION_ID == eventType) {
				DebugLog.Log("sid=="+(String)obj2);
			}*/
        }
    };
}
